"use strict";
/* Controllers */
app.controller("dataMetricAddController", ['$scope', '$location', '$rootScope', '$routeParams', 'dataMetricAddService', function ($scope, $location, $rootScope, $routeParams, dataMetricAddService) {

    var vm = $scope.vm = {};
    vm.r = vm.p = vm.d = {};
    vm.dim = {};

    vm.p.biz = $routeParams.biz;
    vm.p.id = $routeParams.id;

    vm.r.biz = vm.p.biz;
    // 业务方向定义
    vm.d.appNodeDefine = [
        {id:'pingtai_c',name:'ToC'},
        {id:'pingtai_b',name:'ToB'},
        {id:'xiaoshou',name:'ToM'}
    ];
    var selectAppNode = function(id) {
        for(var i=0;i<vm.d.appNodeDefine.length; ++i) {
            var item = vm.d.appNodeDefine[i];
            if(item.id==id) {
                return item;
            }
        }
        return vm.d.appNodeDefine[0];
    };

    // 定义场景字典
    vm.d.bsDefine = bsDictDefine();
    var selectNode = function(arr, name) {
        for(var i=0;i<arr.length; ++i) {
            var item = arr[i];
            if(item.name==name) {
                return item;
            }
        }
        return arr[0];
    };

    vm.d.statusOptDefine = [
        {id:3,name:'03-待上线'},
        {id:4,name:'04-已上线'},
        {id:13,name:'13-已下线'},
        {id:99,name:"99-未定义"}
    ];
    var selectStatusOpt = function(id) {
        for(var i=0;i<vm.d.statusOptDefine.length; ++i) {
            var item = vm.d.statusOptDefine[i];
            if(item.id==id) {
                return item;
            }
        }
        return vm.d.statusOptDefine[0];
    };


    // 主题域定义

    // 业务域定义
//    vm.d.bizAreaDefine = [
//        {en:'zp',name:'HRG指标'},{en:'ganji',name:'赶集指标'}
////        {en:'58', name:'58指标'}
//    ];
//    var selectBizArea = function(en) {
//        for(var i=0;i<vm.d.bizAreaDefine.length; ++i) {
//            var item = vm.d.bizAreaDefine[i];
//            if(item.en==en) {
//                return item;
//            }
//        }
//        return vm.d.bizAreaDefine[0];
//    };

    var errorFn = function (result) {
        alert(result);
    };

    $scope.addMetric = function () {
        var vm = $scope.vm;
        vm.p.biz = 'zp';//vm.p.bizAreaDefine.en;
        vm.p.bizArea = 'HRG指标';//vm.p.bizAreaDefine.name;
        vm.p.statusOpt = vm.p.statusOptEx.id;
        vm.p.topicGroup = vm.p.topicGroupEx.text;
        vm.p.topicGroupEn = readTopicGroupEn(vm.p.topicGroup);//vm.p.topicGroupDefine.en;
        vm.p.topic = vm.p.topicEx.text;
        vm.p.appDiv =vm.p.appNode.id;
        vm.p.userJourneyMap = vm.p.umNode.name;
        vm.p.businessSystem = vm.p.bsNode.name;

        var params = vm.p;
        var procFn = function (response) {
            if (response.success && response.data) {
                alert("执行成功！");
            } else {
                alert(response.msg);
            }
        };
        return dataMetricAddService.addMetric(params, procFn, errorFn);
    };

    $scope.showOptions = function() {
        var vm = $scope.vm;
        vm.p.show = !vm.p.show;
        if(vm.p.show) {
            vm.p.showTxt = "-隐藏";
        }else {
            vm.p.showTxt = "+展开";
        }
    }

    $scope.getMetric = function () {
        var vm = $scope.vm;
        var params = {
            id: vm.p.id
        };

        var procFn = function (response) {
            if (response.success && response.data) {
                vm.p = response.data;
//                vm.p.bizAreaDefine = selectBizArea(vm.p.biz);
                vm.p.statusOptEx = selectStatusOpt(vm.p.statusOpt);
                vm.p.topicGroupEx= findNode(vm.dim.dimTree, vm.p.topicGroup);
                if(typeof(vm.p.topicGroupEx)!='undefined') {
                    vm.dim.selectTopicNodes = vm.p.topicGroupEx.nodes;
                    vm.p.topicEx= findNode(vm.dim.selectTopicNodes, vm.p.topic);
                }
                vm.p.appNode = selectAppNode(vm.p.appDiv);
                vm.p.umNode = selectNode(vm.d.bsDefine[vm.p.appNode.name],vm.p.userJourneyMap);
                vm.p.bsNode = selectNode(vm.p.umNode.bsList,vm.p.businessSystem);

                vm.p.show = false;
                vm.p.showTxt = "+展开";
            } else {
                alert(response.msg);
            }
        };
        return dataMetricAddService.getMetric(params, procFn, errorFn);
    };

    $scope.searchDimDefine = function () {
        var vm = $scope.vm;
        var params = {
            typeCode: '主题划分'
        };
        var procFn = function (response) {
            if (response.success && response.data) {
                //vm.p = JSON.stringify(response.data);
                vm.dim.data = response.data;
                vm.dim.dimTree = dim2Tree(vm.dim.data);
            } else {
                alert(response.msg);
            }
        };
        return dataMetricAddService.searchDimDefine(params, procFn, errorFn);
    };

    $scope.selectTopicGroupChange = function() {
        var vm = $scope.vm;

        var item = findNode(vm.dim.dimTree, vm.p.topicGroupEx.text);
        if(item!=null && typeof(item)!='undefined') {
             vm.dim.selectTopicNodes = item.nodes;
        }
    }
    function findNode(list, text) {
        var item = list.find(function (it) {
            return it.text == text
        });
        return  item;
    }

    $scope.initializeController = function () {
        if(vm.p.id!=0) {
            console.log(vm.p.id);
            $scope.searchDimDefine().then(function(){
                $scope.getMetric();
            });
        }else {
            $scope.searchDimDefine();
            vm.p.indexStd = '';
            vm.p.indexTbl = '';
            vm.p.indexImpl = '-';
            vm.p.indexDesc = '-';
            vm.p.indexSql = '-';
            vm.p.zgroup = '0';
            vm.p.zlevel = 0;
            vm.p.zindex = 0;
            vm.p.statusOptEx = selectStatusOpt(4);
//            vm.p.bizAreaDefine = selectBizArea('zp');
        }
        vm.p.show = false;
        vm.p.showTxt = "+展开";
    }

    $scope.initializeController();
}]);


/* Services */
app.service('dataMetricAddService', ['ajaxService', function (ajaxService) {

    this.addMetric = function (dataMetrics, successFunction, errorFunction) {
        return ajaxService.AjaxPost(dataMetrics, "/star/api/basic-service/v1/metric_define/setMetric.do", successFunction, errorFunction);
    };

    this.getMetric = function (dataMetrics, successFunction, errorFunction) {
        return ajaxService.AjaxPost(dataMetrics, "/star/api/basic-service/v1/metric_define/getMetric.do", successFunction, errorFunction);
    };

    this.searchDimDefine = function(dataMetrics, successFunction, errorFunction) {
        return ajaxService.AjaxPost(dataMetrics, "/star/api/basic-service/v1/dim_define/searchDimDefine.do", successFunction, errorFunction);
    };

}]);



var bsDictDefine = function() {
    var dict = [
        "ToC,01,关注招聘,0101,联盟投放",
        "ToC,01,关注招聘,0102,SEO投放",
        "ToC,02,PUSH召回,0201,PUSH通知",
        "ToC,02,PUSH召回,0202,PUSH唤醒",
        "ToC,03,简历发布,0301,简历发布优化",
        "ToC,03,简历发布,0302,简历质量提升",
        "ToC,04,招聘首页,0401,搜索体验优化",
        "ToC,04,招聘首页,0402,职位分发提效",
        "ToC,05,职位详情,0501,职位分发提效",
        "ToC,06,线上沟通,0601,提高投递转化",
        "ToC,06,线上沟通,0602,提高微聊转化",
        "ToC,06,线上沟通,0603,提高电话转化",
        "ToC,07,个人中心,0701,职位分发提效",
        "ToC,08,我的会员,0801,产品购买监控",
        "ToC,09,求职管理,0901,求职管理提效",
        "ToC,09,求职管理,0902,求职服务转化提效",

        "ToB,01,关注招聘,0101,投放监控",
        "ToB,01,关注招聘,0102,运营监控",
        "ToB,02,企业发布认证,0201,企业发布转化",
        "ToB,02,企业发布认证,0202,企业认证转化",
        "ToB,03,职位发布,0301,职位发布监控",
        "ToB,03,职位发布,0302 职位质量监控",
        "ToB,04,招聘管理,0401,商业推广转化",
        "ToB,05,简历列表,0501,简历分发监控",
        "ToB,06,简历详情,0601,简历分发监控",
        "ToB,07,线上转化,0701,下载转化监控",
        "ToB,07,线上转化,0702,微聊转化监控",
        "ToB,07,线上转化,0703,电话转化监控",
        "ToB,08,会员开通,0801,会员开通监控",
        "ToB,09,会员转化,0901,产品购买监控",
        "ToB,09,会员转化,0902,付款流程",
        "ToB,10,求职者管理,1001,求职者管理监控",

        "ToM,01,销售管理,0101,其他监控",
        "ToM,01,销售管理,0102,线索商机监控",
        "ToM,01,销售管理,0103,合同订单摊销监控",
        "ToM,01,销售管理,0104,客服监控",
        "ToM,01,销售管理,0105,商品流程监控",
        "ToM,01,销售管理,0106,绩效流程监控"
    ];
    // 定义tree的样式
    var tree = {
        "ToB": [],
        "ToC": [],
        "ToM": []// 销售管理
    }
    var findIdx = function(arr, id) {
        for(var i=0; i<arr.length; ++i) {
            if(id==arr[i].id) {
                return i;
            }
        }
        return null;
    }
    for(var i=0; i<dict.length; ++i) {
        var items = dict[i].split(',');
        var row = {group: items[0], id: items[1], name:items[2], bsId: items[3], bsName: items[4]};
        var arr = tree[row.group];
        var currNode = null;
        var idx = findIdx(arr, row.id);
        if(idx==null) {
            currNode = {
                id: row.id,
                name: row.name,
                bsList:[]
            };
            arr.push(currNode);
        }else {
            currNode = arr[idx];
        }
        currNode.bsList.push({
            id:row.bsId,
            name:row.bsName
        });
    }
    console.log(JSON.stringify(tree));
    return tree;
}